﻿using MovingChameLibrary.Models;
using MovingChameLibrary.Models.TourPage;
using MovingChameLibrary.ScheduleServiceReference;
using MovingChameLibrary.ServiceManager;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace MovingChameLibrary.ViewModels
{
    public abstract class TourViewModel : BaseViewModel
    {
        private Tour _tour;

        public TourViewModel(int tourId)
        {
            ServiceFactory.GetTourServiceClient().GetTourAsync(tourId);
            ServiceFactory.GetTourServiceClient().GetTourCompleted += TourViewModel_GetTourCompleted;
        }

        private void TourViewModel_GetTourCompleted(object sender, TourServiceReference.GetTourCompletedEventArgs e)
        {
            _tour = DataConverter.ConvertToTour(e.Result);

            ServiceFactory.GetScheduleServiceClient().GetTourScheduleAsync(_tour.Id);
            ServiceFactory.GetScheduleServiceClient().GetTourScheduleCompleted += TourViewModel_GetTourScheduleCompleted;

            this._pageTitle = _tour.Title;

            this._allGroups = new ObservableCollection<object>();

            TourIntroductionGroup tourOverviewData = new TourIntroductionGroup();
            tourOverviewData.Items.Add(_tour);
            this._allGroups.Add(tourOverviewData);
        }

        private void TourViewModel_GetTourScheduleCompleted(object sender, ScheduleServiceReference.GetTourScheduleCompletedEventArgs e)
        {
            TourScheduleGroup tourSchedule = new TourScheduleGroup();
            foreach (ScheduleData scheduleData in e.Result)
            {
                tourSchedule.Items.Add(DataConverter.ConvertToSchedule(scheduleData, _tour));
            }
            _tour.Schedule = tourSchedule;
            if (_tour.Schedule.Items.Count != 0)
                this._allGroups.Add(_tour.Schedule);
        }

        private string _pageTitle = string.Empty;
        public string PageTitle
        {
            get { return this._pageTitle; }
            set { SetProperty(ref this._pageTitle, value, "PageTitle"); }
        }

        private ObservableCollection<object> _allGroups;
        public ObservableCollection<object> AllGroups
        {
            get { return this._allGroups; }
        }

        protected RelayCommand _favouriteCommand;
        public abstract ICommand FavouriteCommand { get; }
    }
}